package gui.mainFrame.testCreator.parameterEditor;

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import testGenerator.Controller;
import testGenerator.testMethodModel.UserParamTestValue;

/**
 * Provides table model.
 * @author William Whitney
 */
public class ParamTableModel extends AbstractTableModel
{
    private static final long serialVersionUID = 1L;

    private Controller controller;
    private ArrayList<UserParamTestValue> testBindings;

    /**
     * Default constructor.
     * @param controller
     */
    public ParamTableModel(Controller controller)
    {
        this.controller = controller;
        testBindings = controller.getCurrTestPath().userParamTestValues;
    }

    /**
     * Returns the column name.
     * @param column
     * @return
     */
    @Override
    public String getColumnName(int column)
    {
        String columnName = "";
        if (column == 0)
        {
            columnName = "Type";
        }
        else if (column == 1)
        {
            columnName = "Identifier";
        }
        else
        {
            columnName = "Input Value";
        }
        return columnName;
    }

    /**
     * Returns the number of rows.
     * @return
     */
    @Override
    public int getRowCount()
    {
        return this.testBindings.size();
    }

    /**
     * Returns the number of columns.
     * @return
     */
    @Override
    public int getColumnCount()
    {
        return 3;
    }

    /**
     * Gets the value at a particular row, col pos.
     * @param rowIndex
     * @param columnIndex
     * @return
     */
    @Override
    public Object getValueAt(int rowIndex, int columnIndex)
    {
        String retVal = "";
        if (columnIndex == 0)
        {
            retVal = testBindings.get(rowIndex).paramBinding.type;
        }
        else if (columnIndex == 1)
        {
            retVal = testBindings.get(rowIndex).paramBinding.name;
        }
        else if (columnIndex == 2)
        {
            retVal = testBindings.get(rowIndex).userParamValue;
            String type = testBindings.get(rowIndex).paramBinding.type;
            if (isPreloadChar(retVal, type))
            {
                retVal += "'(Input)'";


            }

            if (isPreloadString(retVal, type))
            {
                retVal += "\"(Input)\"";
            }
        }

        return retVal;
    }

    /**
     * Determines which cells are editable.
     * @param rowIndex
     * @param columnIndex
     * @return
     */
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex)
    {
        if (columnIndex == 2)
        {
            return true;
        }
        return false;
    }

    /**
     * Sets a value at a particular cell.
     * @param userExpectedOutput 
     * @param rowIndex
     * @param columnIndex
     */
    @Override
    public void setValueAt(Object userExpectedOutput, int rowIndex, int columnIndex)
    {
        if (columnIndex == 2)
        {
            testBindings.get(rowIndex).userParamValue = (String) userExpectedOutput;
        }
    }

    private boolean isPreloadChar(String retVal, String type)
    {
        if ((type.equals("char") || type.equals("Character")) && retVal.equals(""))
        {
            return true;
        }
        return false;
    }

    private boolean isPreloadString(String retVal, String type)
    {
        if (type.equals("String") && retVal.equals(""))
        {
            return true;
        }
        return false;
    }
}
